<?php

namespace Home\Controller;

use Home\Lib\Uploader;
use Think\Image;

class JokerController extends HomeController
{
    protected $config = array('app_type' => 'master');

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        $siteId = (int) I('site_id');
        if (empty($siteId)) {
            $this->error('请选择要设计的站点', U('site/index'));
        }

        $siteInfo = M('Site')->find($siteId);
        $siteName = $siteInfo['name'];
        $siteStatus = $siteInfo['status'];
        $log = $siteInfo['log'];
        $basePrice = $siteInfo['base_price'];
        $allowEdit = true;
        if (!in_array($siteStatus, ['EDIT', 'RETURN'])) {
            $allowEdit = false;
        }

        $moduleList = [];
        foreach (M('Module')->where('is_del=0')->select() as $row) {
            if ($row['major']) {
                $moduleList[$row['major']][] = $row;
            }
        }

        for ($i = 1; $i <= 6; ++$i) {
            if ($siteInfo['pic_other_'.$i.'']) {
                $siteInfo['pic_other_'.$i.'_thumb'] = './Uploads/thumbs/'.md5($siteInfo['pic_other_'.$i.'']).'.jpg';
            } else {
                if ($allowEdit) {
                    $siteInfo['pic_other_'.$i.'_thumb'] = 'Public/Ins/img/add.png';
                } else {
                    $siteInfo['pic_other_'.$i.''] = 'Public/Ins/img/tower.png';
                    $siteInfo['pic_other_'.$i.'_thumb'] = 'Public/Ins/img/tower.png';
                }
            }
        }

        for ($i = 1; $i <= 9; ++$i) {
            if ($siteInfo['pic_env_'.$i.'']) {
                $siteInfo['pic_env_'.$i.'_thumb'] = './Uploads/thumbs/'.md5($siteInfo['pic_env_'.$i.'']).'.jpg';
            } else {
                if ($allowEdit) {
                    $siteInfo['pic_env_'.$i.'_thumb'] = 'Public/Ins/img/add.png';
                } else {
                    $siteInfo['pic_env_'.$i.''] = 'Public/Ins/img/tower.png';
                    $siteInfo['pic_env_'.$i.'_thumb'] = 'Public/Ins/img/tower.png';
                }
            }
        }

        for ($i = 1; $i <= 2; ++$i) {
            if ($siteInfo['pic_all_'.$i.'']) {
                $siteInfo['pic_all_'.$i.'_thumb'] = './Uploads/thumbs/'.md5($siteInfo['pic_all_'.$i.'']).'.jpg';
            } else {
                if ($allowEdit) {
                    $siteInfo['pic_all_'.$i.'_thumb'] = 'Public/Ins/img/add.png';
                } else {
                    $siteInfo['pic_all_'.$i.''] = 'Public/Ins/img/tower.png';
                    $siteInfo['pic_all_'.$i.'_thumb'] = 'Public/Ins/img/tower.png';
                }
            }
        }

        $this->assign('allowEdit', $allowEdit);
        $this->assign('log', $log);
        $this->assign('siteId', $siteId);
        $this->assign('siteName', $siteName);
        $this->assign('moduleList', $moduleList);
        $this->assign('reason', $siteInfo['reason']);
        $this->assign('summary', $siteInfo['summary']);
        $this->assign('basePrice', $basePrice ? number_format($basePrice, 2, '.', '') : '');
        $this->assign('majorList', array_merge(array_combine(C('MAJOR'), C('MAJOR')), ['其他' => '其他']));
        $this->assign('siteInfo', $siteInfo);
        $this->display();
    }

    public function add()
    {
        $model = D('Joker');

        if ($model->create() !== false and $model->add() !== false) {
            $siteId = (int) $_POST['site_id'];
            $totalPrice = D('JokerView')->where(['site_id' => $siteId])->sum('Joker.total_price');
            D('Site')->save([
                'id' => $siteId,
                'total_price' => $totalPrice,
            ]);
            $this->ajaxReturn([
                'code' => 'ok',
                'msg' => '添加成功！'
            ]);
        } else {
            $this->ajaxReturn([
                'code' => 'fail',
                'msg' => '添加失败！'
            ]);
        }
    }

    public function apply()
    {
        $siteId = (int) I('site_id');
        $reason = I('reason');

        if (D('Site')->changeStatus($siteId, 'IN_ZONE_AGENT1', '', $reason)) {
            $this->ajaxReturn([
                'code' => 'ok',
                'msg' => '申请成功！'
            ]);
        } else {
            $this->ajaxReturn([
                'code' => 'fail',
                'msg' => '申请失败！'
            ]);
        }
    }

    public function groupList()
    {
        $siteId = (int) I('site_id');
        if ($siteId) {
            $data = [];
            foreach (D('JokerView')->where(['site_id' => $siteId])->order('id desc')->select() as $row) {
                if ($row['pic']) {
                    $row['pic_thumb'] = './Uploads/thumbs/'.md5($row['pic']).'.jpg';
                } else {
                    $row['pic'] = 'Public/Ins/img/tower.png';
                    $row['pic_thumb'] = 'Public/Ins/img/tower.png';
                }
                $data[$row['major']][] = $row;
            }

            $this->ajaxReturn([
                'code' => 'ok',
                'msg' => '获取成功！',
                'data' => $data,
            ]);
        } else {
            $this->ajaxReturn([
                'code' => 'fail',
                'msg' => '获取失败！'
            ]);
        }
        # code...
    }

    public function del()
    {
        $idList = I('id');
        if (D('Joker')->where(['id' => ['in', array_filter($idList)]])->delete()) {
            $siteId = (int) $_POST['site_id'];
            $totalPrice = D('JokerView')->where(['site_id' => $siteId])->sum('Joker.total_price');
            D('Site')->save([
                'id' => $siteId,
                'total_price' => $totalPrice,
            ]);
            $this->ajaxReturn([
                'code' => 'ok',
                'msg' => '删除成功！',
            ]);
        } else {
            $this->ajaxReturn([
                'code' => 'fail',
                'mas' => '删除失败！'
            ]);
        }
    }

    public function upload($hq = false)
    {
        $uploader = new Uploader([
            'path' => C('UPLOAD_PATH'),
            'maxSize' => 4194304,
        ]);

        if (!isset($_FILES['file'])) {
            exit;
        }

        $qut = $hq ? 300 : 150;

        $result = $uploader->upload($_FILES['file']);
        if ($result['code'] == 'ok') {
            $image = new Image();
            $image->open($result['src']);
            $image->thumb(150, 150)->save(C('UPLOAD_PATH').'thumbs/'.md5($result['src']).'.jpg');

            die(json_encode([
                'code' => 'ok',
                'msg' => '上传成功！',
                'src' => $result['src'],
            ]));
        } else {
            die(json_encode($result));
        }
    }
}
